﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace GMS.Core.Config.Models
{
    /// <summary>
    /// 数据库配置
    /// </summary>
    [Serializable]
    public class DaoConfig : ConfigFileBase
    {
        public DaoConfig()
        {
        }
        #region 序列化属性
        public string MyDb { get; set; }
        public string MyDbLog { get; set; }

        #endregion
        #region 数据库名称
        public string MyDbCatalog
        {
            get { return GetCatagoryValue(MyDb.Split(';').ToList()); }
        }

        public string MyDbLogCatalog
        {
            get { return GetCatagoryValue(MyDbLog.Split(';').ToList()); }
        }
      
        #endregion
        /// <summary>
        /// 获取数据库名称
        /// </summary>
        /// <param name="config">config内容</param>
        private string GetCatagoryValue(List<string> config)
        {
            var catagoryName = "";
            var catalogValue = config.Where(n => n.Contains("Initial Catalog"));
            if (catalogValue.Any())
            {
                var catagoryNames = catalogValue.FirstOrDefault().Split('=');
                if (catagoryNames.Length >= 2)
                {
                    catagoryName = catagoryNames[1];
                }
            }
            return catagoryName+ ".dbo.";
        }
    }
}
